package com.cdwl.tiyanke_java.service;

import com.cdwl.tiyanke_java.mapper.UserMapper;
import com.cdwl.tiyanke_java.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public Map<String, Object> selectAll() {
        List<User> users = userMapper.selectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询所有用户成功");
        map.put("data", users);
        return map;
    }

    public Map<String, Object> login(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        User user = userMapper.login(username);
        if(user == null) {
            // 如果user等于null，代表数据库没有此用户名
            map.put("code", 505);
            map.put("msg", "用户名或者密码错误");
        }
        else {
            if(user.getPassword().equals(password)) {
                // 如果存在用户名，则判断密码是否正确，如果正确则代表登录成功
                map.put("code", 200);
                map.put("msg", "登录成功");
            }
            else {
                map.put("code", 505);
                map.put("msg", "用户名或者密码错误");
            }
        }
        return map;
    }
}
